package cn.enilu.flash.mobile.controller;

import cn.enilu.flash.bean.entity.bus.Collection;
import cn.enilu.flash.bean.enumeration.bus.ColType;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.mobile.domain.ColDomain;
import cn.enilu.flash.service.bus.CollectionService;
import cn.enilu.flash.web.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/collect")
public class CollectionController extends BaseController {

    @Autowired
    private CollectionService collectionService;

    @PostMapping("/doCollect")
    public Object doCollect(@RequestBody ColDomain colDomain) {
        Long idUser = getIdUser();
        String cub = colDomain.getColType() + "_" + idUser + "_" + colDomain.getBusId();
        List<Collection> colUidBid = collectionService.queryAll(SearchFilter.build("colUidBid", SearchFilter.Operator.EQ, cub));
        if (CollectionUtils.isEmpty(colUidBid)) {
            if (colDomain.getState() == 1) {
                Collection collection = new Collection();
                collection.setBusId(colDomain.getBusId().longValue());
                collection.setColType(ColType.PREMISES.getCol());
                collection.setUserId(idUser);
                collection.setColUidBid(cub);
                collectionService.insert(collection);
            }
        } else {
            if (colDomain.getState() == 0) {
                collectionService.deleteAll(colUidBid);
            }
        }
        return Rets.success();
    }

    @GetMapping("/check/collect")
    public Object checkCollect(@RequestParam Long busId, @RequestParam String colType) {
        Long idUser = getIdUser();
        String cub = "2_" + idUser + "_" + busId;
        List<Collection> colUidBid = collectionService.queryAll(SearchFilter.build("colUidBid", SearchFilter.Operator.EQ, cub));
        return Rets.success(!CollectionUtils.isEmpty(colUidBid));
    }

}
